import 'package:sow_app/repository/model/book_classify_model.dart';
import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import 'user_info_model.dart';

class NovelModel {
  final String novelId;
  final UserInfoModel user;
  final String title;
  final List<BookClassifyModel> novelTypeList;
  final String? bookStatus;
  final String? wordCount;
  final String? visitCount;
  final String? score;
  final String? bookDesc;
  final String? isVip;
  final String? lastIndexUpdateTime;
  final String cover;

  NovelModel.fromJson(Map<String, dynamic> json)
      : novelId = json['id'],
        user = UserInfoModel.fromJson(
            {"username": json["authorId"], "nickName": json["authorName"]}),
        title = json['bookName'],
        novelTypeList = ((json['catList'] ?? []) as List)
            .map((e) => BookClassifyModel.fromJson(e))
            .toList(),
        bookStatus = json['bookStatus'],
        bookDesc = json['bookDesc'],
        wordCount = json['wordCount'],
        visitCount = json['visitCount'],
        score = json['score'],
        isVip = json['isVip'],
        lastIndexUpdateTime = json['lastIndexUpdateTime'],
        cover = json['cover'] ??
            'http://${I18n.backend_ip}:${I18n.backend_port}${I18n.base_cover_url}';

  Map<String, dynamic> toJson() => {
        'id': novelId,
        "authorId": user.userName,
        "authorName": user.nickName,
        'bookName': title,
        'catList': novelTypeList.map((e) => e.toJson()).toList(),
        'wordCount': wordCount,
        'visitCount': visitCount,
        'isVip': isVip,
        'lastIndexUpdateTime': lastIndexUpdateTime,
        'cover': cover
      };

  toEntity() => Novel(
      novelId: novelId,
      title: title,
      author: user.nickName,
      wordCount: wordCount,
      novelTypeList: novelTypeList.map<Classify>((e) => e.toEntity()).toList(),
      bookStatus: bookStatus,
      visitCount: visitCount,
      lastUpdateTime: lastIndexUpdateTime,
      isVip: isVip,
      score: score,
      cover: cover);
}
